package com.mvs.sillybat;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class WorldRenderer {
	static final float FRUSTUM_WIDTH = 10;
	static final float FRUSTUM_HEIGHT = 15;
	World world;
	OrthographicCamera cam;
	SpriteBatch batch;

	public WorldRenderer(SpriteBatch batch, World world) {
		this.world = world;
		this.cam = new OrthographicCamera(FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		this.cam.position.set(FRUSTUM_WIDTH / 2, FRUSTUM_HEIGHT / 2, 0);
		this.batch = batch;
	}

	public void render() {
		if (world.robot.position.y > cam.position.y)
			cam.position.y = world.robot.position.y;
		cam.update();
		batch.setProjectionMatrix(cam.combined);
		renderBackground();
		renderObjects();
	}

	public void renderBackground() {
		batch.disableBlending();
		batch.begin();
		batch.draw(Assets.backgroundRegion, cam.position.x - FRUSTUM_WIDTH / 2,
				cam.position.y - FRUSTUM_HEIGHT / 2, FRUSTUM_WIDTH,
				FRUSTUM_HEIGHT);
		batch.end();
	}

	public void renderObjects() {
		batch.enableBlending();
		batch.begin();
		
		renderRobot();
		renderPlatforms();
		batch.end();
	}

	private void renderRobot() {
		Texture keyFrame;
		switch (world.robot.state) {
		case Robot.ROBOT_STATE_FALL:
			keyFrame = Assets.Bot2;
			break;
		case Robot.ROBOT_STATE_JUMP:
			keyFrame = Assets.Bot1;// Assets.bobJump.getKeyFrame(world.robot.stateTime,
									// Animation.ANIMATION_LOOPING);
			break;
		// case Bob.BOB_STATE_HIT:
		default:
			keyFrame = Assets.Bot1;
		}

		float side = 0;
		if (world.robot.velocity.x < 0.2f)
			side = -1;
		else if (world.robot.velocity.x > 0.2f)
			side = 1;

		//if (side < 0)
		if(world.robot.state==Robot.ROBOT_STATE_JUMP)
			{batch.draw(keyFrame, world.robot.position.x - world.robot.ROBOT_WIDTH/2,
					world.robot.position.y - world.robot.ROBOT_HEIGHT/2,world.robot.ROBOT_WIDTH, world.robot.ROBOT_HEIGHT);
			
			batch.draw(Assets.jumpFire, 0.2f+world.robot.position.x - world.robot.ROBOT_WIDTH/2,
					world.robot.position.y - world.robot.ROBOT_HEIGHT*3/4,world.robot.ROBOT_WIDTH/3, world.robot.ROBOT_HEIGHT/4);
			batch.draw(Assets.jumpFire, 0.6f+world.robot.position.x - world.robot.ROBOT_WIDTH/2,
					world.robot.position.y - world.robot.ROBOT_HEIGHT*3/4,world.robot.ROBOT_WIDTH/3, world.robot.ROBOT_HEIGHT/4);
			}
		else if(world.robot.state==Robot.ROBOT_STATE_FALL)
			batch.draw(keyFrame, world.robot.position.x - world.robot.ROBOT_WIDTH*1.5238f/2,
					world.robot.position.y - world.robot.ROBOT_HEIGHT/2,world.robot.ROBOT_WIDTH*1.5238f, world.robot.ROBOT_HEIGHT);
		//else
		//	batch.draw(keyFrame, world.robot.position.x - 0.5f,
			//		world.robot.position.y - 0.5f, side * 1, 1);
	}

	private void renderPlatforms() {
		int len = world.platforms.size();
		for (int i = 0; i < len; i++) {
			Platform platform = world.platforms.get(i);
			Texture keyFrame = Assets.platform;
			batch.draw(keyFrame, platform.position.x - Platform.PLATFORM_WIDTH
					/ 2, platform.position.y - 0.25f, Platform.PLATFORM_WIDTH,
					Platform.PLATFORM_HEIGHT);
		}
	}

	//
}
